import { ActionService } from './action.service';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/toPromise';
import { UtilsService } from './utils/utils.service';
import { environment } from './../../../environments/environment';

export class AccountModel {
      Claims: any[];
      EmployeeID: string;
      ManageHouses: any[];
      UserDepartmentPosts: any[];
}

@Injectable()
export class AccountService {

      private constAccount: AccountModel = new AccountModel();

      constructor(
            private utils: UtilsService,
            private actionService: ActionService
      ) { }

      //获取用户信息
      getAccountInfo(): Promise<AccountModel> {

            //账号信息->内存
            let constInfo = this.constAccount;
            if (!this.utils.isNull(constInfo)) {
                  // alert('constAccount');
                  return Promise.resolve(this.constAccount);
            }

            //账号信息->API
            return this.accountApi();
      }

      //更新用户信息
      updateAccountInfo(): Promise<AccountModel> {
            this.constAccount = null;
            return this.getAccountInfo();
      }

      //登陆成功 获取用户信息
      private accountApi(): Promise<AccountModel> {
            let userInfoApi = environment.flmApi + "/house/Admin/GetEmployeeInfo";

            return this.actionService.post(userInfoApi)
                  .toPromise()
                  .then(res => {
                        //账号信息->内存
                        this.constAccount = res.Result;
                        return this.constAccount;
                  })
                  .catch(error => error);

      }
}